package me.yiyou.meihua.ui.page.record

import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewModelScope
import com.kongzue.dialogx.dialogs.PopTip
import me.yiyou.meihua.MyApplication
import me.yiyou.meihua.data.BaGuaImage
import me.yiyou.meihua.data.LunarDate
import me.yiyou.meihua.data.SaveBean
import me.yiyou.meihua.utils.LogUtil
import me.yiyou.meihua.utils.getMeiHuaDongYao
import me.yiyou.meihua.utils.getMeiHuaShang
import me.yiyou.meihua.utils.getMeiHuaXia

class RecordViewModel : ViewModel() {
    // 注入repository
    val meiHuaRepository = MyApplication.appContext.meiHuaRepository

    // 占问事,时间
    var content: String = ""
    var time: String = ""

    // 卦象数据
    val shangGua = mutableIntStateOf(0)
    val xiaGua = mutableIntStateOf(0)
    val dongYao = mutableIntStateOf(0)

    // 位置下标
    private val _position = mutableStateOf(0)
    val position = _position

    // 所有记录,直接暴漏
    val allList: LiveData<List<SaveBean>> = meiHuaRepository.allList
        .asLiveData(viewModelScope.coroutineContext)


    /**
     * 下一卦
     */
    fun nextGua() {
        if (_position.value >= allList.value!!.size - 1) {
            PopTip.show("已经到底了~")
            return
        }
        _position.value++
    }

    /**
     * 上一卦
     */
    fun previousGua() {
        if (_position.value <= 0) {
            PopTip.show("已经是第一卦了~")
            return
        }
        _position.value--
    }

    /**
     * 起卦
     */
    fun qiGua(saveBean: SaveBean) {
        // 获取卦
        shangGua.intValue = saveBean.shang
        xiaGua.intValue = saveBean.xia
        // 动爻
        dongYao.intValue =saveBean.dong
        content = saveBean.content?:""
        time = saveBean.time?:""
        LogUtil.e("GuaValue: ${shangGua.intValue}, xiaGua: ${xiaGua.intValue}, dongYao: ${dongYao.intValue}")
    }

    /**
     * 画卦
     */
    fun paintGua(shang: Int, xia: Int, dong: Int? = null): BaGuaImage {
        var baguaImage: BaGuaImage = BaGuaImage(false, false, false, false, false, false)
        when (shang) {
            1 -> {
                baguaImage.six = true
                baguaImage.five = true
                baguaImage.four = true
            }

            2 -> {
                baguaImage.six = false
                baguaImage.five = true
                baguaImage.four = true
            }

            3 -> {
                baguaImage.six = true
                baguaImage.five = false
                baguaImage.four = true
            }

            4 -> {
                baguaImage.six = false
                baguaImage.five = false
                baguaImage.four = true
            }

            5 -> {
                baguaImage.six = true
                baguaImage.five = true
                baguaImage.four = false
            }

            6 -> {
                baguaImage.six = false
                baguaImage.five = true
                baguaImage.four = false
            }

            7 -> {
                baguaImage.six = true
                baguaImage.five = false
                baguaImage.four = false
            }

            0 -> {
                baguaImage.six = false
                baguaImage.five = false
                baguaImage.four = false
            }
        }
        when (xia) {
            1 -> {
                baguaImage.three = true
                baguaImage.two = true
                baguaImage.one = true
            }

            2 -> {
                baguaImage.three = false
                baguaImage.two = true
                baguaImage.one = true
            }

            3 -> {
                baguaImage.three = true
                baguaImage.two = false
                baguaImage.one = true
            }

            4 -> {
                baguaImage.three = false
                baguaImage.two = false
                baguaImage.one = true
            }

            5 -> {
                baguaImage.three = true
                baguaImage.two = true
                baguaImage.one = false
            }

            6 -> {
                baguaImage.three = false
                baguaImage.two = true
                baguaImage.one = false
            }

            7 -> {
                baguaImage.three = true
                baguaImage.two = false
                baguaImage.one = false
            }

            0 -> {
                baguaImage.three = false
                baguaImage.two = false
                baguaImage.one = false
            }
        }

        return baguaImage
    }

    /**
     * 插入数据库
     */
    suspend fun insert(saveBean: SaveBean) {
        try {
            meiHuaRepository.insert(saveBean)
        } catch (e: Exception) {
            LogUtil.e("插入失败: ${e.message}")
        }
    }
}